package cn.devices.service.impl;

import cn.devices.dto.UserAuthDto;
import cn.devices.entity.TbAuthUser;
import cn.devices.entity.TbType;
import cn.devices.mapper.TbAuthUserMapper;
import cn.devices.model.TbDmModel;
import cn.devices.service.TbAuthUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备个人授权表 服务实现类
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Service
public class TbAuthUserServiceImpl implements TbAuthUserService {

    @Autowired
    private TbAuthUserMapper tbAuthUserMapper;

    @Override
    public int tbAuthUserAdd(TbAuthUser tbAuthUser) {
        return tbAuthUserMapper.tbAuthUserAdd(tbAuthUser);
    }

    @Override
    public int tbAuthUserDelete(String deviceCode, String userId) {
        return tbAuthUserMapper.tbAuthUserDelete(deviceCode, userId);
    }

    @Override
    public PageInfo<UserAuthDto> updateUserList(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        
        List<UserAuthDto> list = tbAuthUserMapper.userList();
        List<TbAuthUser> tbAuthUserList=tbAuthUserMapper.authUserList();
        List<TbDmModel> tbDmModel=tbAuthUserMapper.tbDmModel();
        for (UserAuthDto user:list
             ) {
            if (user.getTbAuthDmList()==null) user.setTbAuthDmList(new ArrayList<>());
            if (user.getTbAuthUserList()==null) user.setTbAuthUserList(new ArrayList<>());
            for (TbAuthUser tbAuthUser:tbAuthUserList
                 ) {
                if (user.getUserId().equals(tbAuthUser.getUserId())){
                    user.getTbAuthUserList().add(tbAuthUser);
                }
            }
            for (TbDmModel ts : tbDmModel){
                if (user.getDepCode().equals(ts.getDepCode())){
                    TbType tbType=new TbType();
                    tbType.setTypeId(ts.getTypeId());
                    tbType.setTypeName(ts.getTypeName());
                    tbType.setTypeLevel(ts.getTypeLevel());
                    user.getTbAuthDmList().add(tbType);
                }
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public List<TbAuthUser> userAuthUserById(String userId) {
        return tbAuthUserMapper.userAuthUserById(userId);
    }
}
